<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;


use App\Common\Sql;
use App\Common\Tool;
use App\Exceptions\ApiException;
use App\Logic\WXOrderLogic;

class WeChatApplyPay extends Base
{

    /**
     * 购买钻石微信下单
     * @return string
     * @throws ApiException
     */
    public function logic()
    {
        if (!isset($this->param['goods_id'])) {
            throw new ApiException("goods_id must needed.\n", 351);
        } else {
            $goods_id = $this->param['goods_id'];
        }

        $row = Sql::select('t_diamonds_mall_conf', [['id', $goods_id]]);
        if (count($row) == 0) {
            return $this->returnError(352, "goods id not exeist.");
        }
        $rmb = floatval($row[0]['rmb']);
        //$rmb = 0.01;
        $name = $row[0]['name'];


        $out_trade_no = WXOrderLogic::instance()->createOrderId();
        $ret_arr      = WXOrderLogic::instance()->createOrder($out_trade_no, $rmb, $name, time());
        Tool::createPayInfo($out_trade_no, $rmb, $this->uid, 1, 1, $row[0]['id']);

        return $this->returnData($ret_arr);
    }

}